package com.example.lenovo.downloaddemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.activity.AppStart;
import com.example.lenovo.downloaddemo.activity.MoreActivity;
import com.example.lenovo.downloaddemo.activity.ProductInfoActivity;
import com.example.lenovo.downloaddemo.activity.WebViewActivity;
import com.example.lenovo.downloaddemo.bean.HomeBean;
import com.example.lenovo.downloaddemo.http.AppConfig;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;
import com.example.lenovo.downloaddemo.util.TextStyleLanguage;
import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class HomeAdapter extends BaseAdapter {
    private Context context;
    private List<HomeBean.DataBean> data;
    private RecyclerViewAdapter adapter;

    public HomeAdapter(Context context,List<HomeBean.DataBean> data){
        this.data=data;
        this.context=context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        ViewHolder holder=null;
        if(view==null){
            holder=new ViewHolder();
            view=LayoutInflater.from(AppContext.getInstance()).inflate(R.layout.home_item,null);
            holder.type_Name=(TextView) view.findViewById(R.id.type_Name);
            holder.more=(TextView) view.findViewById(R.id.more);
            holder.image_Bottom=(ImageView) view.findViewById(R.id.image_bottom);
            holder.recyclerView=(RecyclerView) view.findViewById(R.id.recyclerView);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }
            if(!TextStyleLanguage.isEngLish()){
                holder.type_Name.setText(data.get(i).getName());
            }else{
                holder.type_Name.setText(data.get(i).getName_en());
            }
        Picasso.with(context)
                .load(AppConfig.IP+data.get(i).getImg())
                .into(holder.image_Bottom);
        LinearLayoutManager lm=new LinearLayoutManager(context);
        lm.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.recyclerView.setLayoutManager(lm);
        Log.e("----",data.get(i).getGoodsList().size()+"");
        adapter=new RecyclerViewAdapter(data.get(i).getGoodsList());
        holder.recyclerView.setAdapter(adapter);

        final List<HomeBean.DataBean.GoodsListBean> goodsList=data.get(i).getGoodsList();
        adapter.setOnItemClickLitener(new RecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
//                ToastUtil.makeText(AppContext.getInstance(),goodsList.get(position).getGood_id()+"");
                Intent intent=new Intent(AppContext.getInstance(),ProductInfoActivity.class);
                intent.putExtra("goodsid",goodsList.get(position).getGood_id());
                Log.e("homeAdpter",goodsList.get(position).getGood_id()+"");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(AppContext.getInstance(),MoreActivity.class);
                intent.putExtra("c_id",data.get(i).getC_id());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context. startActivity(intent);
            }
        });

        holder.image_Bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(AppContext.getInstance(),WebViewActivity.class);
                intent.putExtra("url",data.get(i).getImg_url());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        return view;
    }

    static class ViewHolder{
        TextView type_Name;
        TextView more;
        RecyclerView recyclerView;
        ImageView image_Bottom;
    }

}
